﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using Com.Toook.Common;
using Com.Toook.ParseHtml;
using System.Text;

public partial class article_content : BaseShowPage
{
    protected string ArticleTitle = string.Empty;
    protected string ArticleContent = string.Empty;
    protected string Author = string.Empty;
    protected string SourceSite = string.Empty;
    protected string ClickCount = string.Empty;
    protected string LikeCount = string.Empty;
    protected string Score = string.Empty;
    protected string PageHtml = string.Empty;
	protected string PreNext = "";//前一条、后一条

    protected void Page_Load(object sender, EventArgs e)
    {
        InitContent();
    }
    private void InitContent()
    {
        string typePageId = Request["id"];
        string page = Request["page"];
        int n=0, p=0;
        int id = 0;
        string type = "";
        string startId = "1";//上一条的Id
        string endId = "3";//下一条的Id
        int currentId = 2;
        bool isValidId = false;//参数是否合法、正确
        if (string.IsNullOrEmpty(page) || !int.TryParse(page, out p))
        {
            page = "1";
        }
        string condition = "";
		string sqlPrevNext = null;
        if (!string.IsNullOrEmpty(typePageId) && typePageId.Length == 9 && typePageId.StartsWith("6") && int.TryParse(typePageId, out id))
        {
            if (id > 610000000 && id < 670000000)
            {
                type = typePageId.Substring(0, 2);
                string current = typePageId.Substring(2, 5);//取出中间5位真正的记录序号
                int num = ParamHelper.ParseIntPositive(current);
                if (num > 0)
                {
                    startId = type + (num - 1).ToString("D5");// + "00";
                    endId = type + (num + 1).ToString("D5");// + "99";					
                    isValidId = true;//参数正确
                    currentId = id;
                }
            }
        }        
		
        if (isValidId)
        {
            condition = " typePageId=" + typePageId + " ";
			sqlPrevNext = "select typePageId, title from PublishArticle where typePageId>=" + 
						startId + "00 and typePageId<=" + startId + "99" +
						" union all " +
						"select typePageId, title from PublishArticle where typePageId>=" + 
						endId + "00 and typePageId<=" + endId + "99";
        }
        else
        {
            DataRow row = PageData.SelectParamRow("typeCode='article'");
            int total = 0;
            int.TryParse(row[PageColumn.typeTotalCount].ToString().Substring(2), out total);
            startId = (total - 1).ToString();//只要下一条
            endId = total.ToString();
            currentId = total;//取最后一条
            condition = " Id=" + currentId + " ";
            page = "1";
			sqlPrevNext = "select typePageId, title from PublishArticle where Id=" + startId;
        }

        string sql;
        if (page == "1")//第一页从PublishArticle取
        {
            sql = "select typePageId, title, content, author, sourceSite, totalPage, score, clickCount, likeCount " +
                " from PublishArticle where " + condition;
        }
        else//后面的页在PublishArticleMorePage中
        {
            sql = "select typePageId, title, content, author, sourceSite, totalPage  from PublishArticleMorePage where " +
                condition + " and pageNum=" + page;
        }
        OleDbConnection conn = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        int totalPage = 1;
        OleDbDataReader reader = null;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            if(reader.Read())
            {
                ArticleContent = reader["content"].ToString();
                ArticleTitle = reader["title"].ToString();
                Author = reader["author"].ToString() == "" ? "" : "作者：" + reader["author"].ToString();
                SourceSite = reader["sourceSite"].ToString() == "" ? "" : "来源：" + reader["sourceSite"].ToString();
                if (p < 2)//第一页才有评分、点击等
                {
                    Score = reader["score"].ToString();
                    ClickCount = reader["clickCount"].ToString();
                    LikeCount = reader["likeCount"].ToString();
                }
                int.TryParse(reader["totalPage"].ToString(), out totalPage);
            }
            reader.Close();
			//查询上一页、下一页
			cmd.CommandText = sqlPrevNext;
			reader = cmd.ExecuteReader();			
			while(reader.Read())
			{
				if(!isValidId)
				{
					PreNext = "下一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
				}
				else
				{
					int.TryParse(reader["typePageId"].ToString(), out n);
					if(n < currentId)
					{
						PreNext += "下一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
					}
					else
					{
						PreNext += "上一条：<a href=\"content.aspx?id=" + reader["typePageId"].ToString() + "\">" + reader["title"].ToString() + "</a>";
					}
				}
			}
			reader.Close();
        }
        catch(Exception exRead)
        {
            LogHelper.Log.Error("读取文章详细内容出错。id=" + typePageId, exRead);
        }
        finally
        {
            AccessHelper.CloseDbResource(conn, reader);
        }
        if (totalPage > 1)
        {
            StringBuilder sb = new StringBuilder(256);
            for (int i = 1; i <= totalPage; i++)
            {
                sb.Append("<a href=\"content.aspx?id=" + typePageId + "&page=" + i.ToString() + "\"");
                if (i == p)
                {
                    sb.Append(" class=\"currentPage\"");
                }
                sb.Append(">" + i.ToString());
                sb.Append("</a>");
            }
            PageHtml = sb.ToString();
        }
    }
}

